package net.atomarrow.sy.permission;

import java.lang.reflect.Method;

import net.atomarrow.action.ActionInvocation;
import net.atomarrow.sy.annotation.FuncPermission;
import net.atomarrow.sy.annotation.Permission;
import net.atomarrow.sy.annotation.UnCheck;
import net.atomarrow.sy.bean.User;
import net.atomarrow.sy.staticvariable.KeySv;

/**
 * 权限检查
 * 
 * @author Michael
 * 
 */
public class SecurityCheck {
    
    public static boolean isNoAnnotation(Method method){
        if (method.getAnnotations()==null||method.getAnnotations().length==0) {
            return true;
        }
        return false;
    }
    public static boolean isUncheck(Method method){
        if (method.isAnnotationPresent(UnCheck.class)) {
            return true;
        }
        return false;
    }
    
    public static boolean isPermissionCheck(Method method){
        if (method.isAnnotationPresent(Permission.class)) {
            return true;
        }
        return false;
    }
    public static boolean isFuncPermissionCheck(Method method){
        if (method.isAnnotationPresent(FuncPermission.class)) {
            return true;
        }
        return false;
    }
    public static boolean permissionCheck(ActionInvocation invocation,Method method) {
        
        Object user = invocation.getController().getSession().getAttribute(KeySv.SESSION_KEY_USER);
        if (user == null||!(user instanceof User)) {
            return false;
        }
        int r = ((User) user).getRole();
        Permission per = method.getAnnotation(Permission.class);
        int[] roles = per.value();
        for (int i : roles) {
            if(i==Role.LOGIN){
                return true;
            }
            if (i == r) {
                return true;
            }
        }
        return false;
    }

}
